package poiDemo;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileOutputStream;

/**
 * @author Martin MYZ
 * @create 2019-07-15-19:27
 */
public class PoiDemo {

    @Test
    public void fun01() throws Exception {
        //1. 创建工作博对象
        XSSFWorkbook workbook = new XSSFWorkbook("F:\\abc.xls");
        //2. 获得工作表对象
        XSSFSheet sheet = workbook.getSheetAt(0);
        //3. 遍历工作表,获得行对象
        for (Row row : sheet) {
            //4. 遍历行对象,获得列对象
            for (Cell cell : row) {
                //5. 获得列里面的内容
                System.out.println(cell.getStringCellValue());
            }
            System.out.println("---------------");
        }
        //6.关闭
        workbook.close();
    }

    @Test
    public void fun02() throws Exception{
        //1. 创建工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook();
        //2. 创建工作表对象
        XSSFSheet sheet = workbook.createSheet("学生名单");
        //3.创建行
        XSSFRow row01 = sheet.createRow(0);
        //4. 创建列,设置内容
        row01.createCell(0).setCellValue("姓名");
        row01.createCell(1).setCellValue("年龄");
        row01.createCell(2).setCellValue("性别");

        XSSFRow row02 = sheet.createRow(1);
        row02.createCell(0).setCellValue("张三");
        row02.createCell(1).setCellValue("18");
        row02.createCell(2).setCellValue("男");

        XSSFRow row03 = sheet.createRow(2);
        row03.createCell(0).setCellValue("李四");
        row03.createCell(1).setCellValue("19");
        row03.createCell(2).setCellValue("女");


        //通过输出流对象,写到磁盘
        FileOutputStream os = new FileOutputStream("F:/student.xls");
        workbook.write(os);
        os.flush();
        os.close();

        workbook.close();
    }


}
